/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
base_url = "http://localhost/activty/ci/";
var editor = null;


$(document).ready(function(){
    //base_url = $("#base_url").val();
    editor = $("textarea").kendoEditor();


    var  txt_activity_end_reg_date = $(" input[name=txt_activity_end_reg_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
           
    }});

    // วันเริ่มกิจกรรม 
    var  txt_activity_start_reg_date = $(" input[name=txt_activity_start_reg_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
            txt_activity_end_reg_date.datepicker("option", "minDate", new Date(datetext));
    }});
    //วันสิ้นสุดลงทะเบียน:
    var  txt_activity_end_date = $(" input[name=txt_activity_end_date] ").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
            txt_activity_start_reg_date.datepicker("option", "minDate", new Date(datetext));
    }});
    //วันเปิดลงทะเบียน 
    var  txt_activity_start_date = $("input[name=txt_activity_start_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
            txt_activity_end_date.datepicker("option", "minDate", new Date(datetext));
    }});
    
    if($("#date_now").length > 0){
         txt_activity_start_date.datepicker("option", "minDate", new Date($("#date_now").val()));
    }

    
    $("#bAddMajors").click(bAddMajors_Click);
    $("#bAddTecher").click(bAddTecher_Click);
    $("#bAddMajors_1").click(bAddMajors_Click_create);
    $("#bAddTecher_1").click(bAddTecher_Click_create);
    _search_activity();
});


            function bAddMajors_Click_create(){
                getMajorDialog({onSelect : function(item){
                       
                        var item_html = '<div class="major_item">' + 
                                                  '    <img  onclick="remove_major_item_create(this)"  class="img-close-icon"  src="'  + base_url + 'res/images/close.png"   />' +
                                                  item.title +  "  ชั้นปีที่  " +
                                                  '<input  class="major_ch"   name="major_data_ch[]"  type="hidden" value="'  + item.id +  '"  />' +
                                                  '<input  class="year_class_ch"  name="year_class[]"  type="hidden" value="'  + item.classnumber +  '"  />'
                                                 '</div>';
                              if($("input[type=hidden][class=major_ch][value="+ item.id +  "]:eq(0)").length == 0){
                                 $(item_html).appendTo("#dv-major-dataset");
                              }
                },onsubmit : function(){

                }});
            }
            
            function bAddTecher_Click_create(){
                
                        getTeachersDialog({onSelect : function(item){
                                var item_html = '<div class="major_item">' + 
                                                          '    <img  onclick="remove_major_item_create(this)"  class="img-close-icon"  src="'  + base_url + 'res/images/close.png"   />' +
                                                          item.title +
                                                          '<input class="teacher_ch" name="teacher_data_ch[]"  type="hidden" value="'  + item.id +  '"  />'
                                                         '</div>';
                                      if($("input[type=hidden][class=teacher_ch][value="+ item.id +  "]:eq(0)").length == 0){
                                         $(item_html).appendTo("#dv-teacher-dataset");
                                      }
                        },onsubmit : function(){

                        }});
            }
            
function remove_major_item_create(obj_img){
     $(obj_img).parents(".major_item:eq(0)").fadeOut(800,function(){
                $(this).remove();
     });
}

function bAddTecher_Click(){
    getTeachersDialog({onSelect : function(item){
            var item_html = '<div class="major_item">' + 
                                      '    <img  onclick="remove_major_item(this)"  class="img-close-icon"  src="'  + base_url + 'res/images/close.png"   />' +
                                      item.title +
                                      '<input class="teacher_ch" name="teacher_data_ch[]"  type="hidden" value="'  + item.id +  '"  />'
                                     '</div>';
                  if($("input[type=hidden][class=teacher_ch][value="+ item.id +  "]:eq(0)").length == 0){
                     $(item_html).appendTo("#dv-teacher-dataset");
                  }
    },onsubmit : function(){
        
    }});
}

function remove_major_item(major_relation_id,obj_img){
    if(major_relation_id=="")return;
    if(!confirm("ยืนยันการลบข้อมูล"))return false;
    $.post(base_url + "activity/unset_major_of_activity",{"major_relation_id" : major_relation_id , "rnd" : Math.random()},function(data){
            alert(data);
        var item = $(obj_img).parents(".major_item:eq(0)");
            item.fadeOut(800,function(){
                item.remove();
            })
    });
}

function bAddMajors_Click(){
    getMajorDialog({onSelect : function(item){
            var item_html = '<div class="major_item">' + 
                                      '    <img  onclick="remove_major_item(this)"  class="img-close-icon"  src="'  + base_url + 'res/images/close.png"   />' +
                                      item.title +
                                      '<input  class="major_ch"  name="major_data_ch[]"  type="hidden" value="'  + item.id +  '"  />'
                                     '</div>';
                  if($("input[type=hidden][class=major_ch][value="+ item.id +  "]:eq(0)").length == 0){
                     $(item_html).appendTo("#dv-major-dataset");
                  }
    },onsubmit : function(){
        
    }});
}

function getMajorDialog(object_vars){
    $.post(base_url + "major/get_major_dialog",{"rnd" : Math.random()},function(data){
        $("#wiged-area2").html("");
        $(data).appendTo("#wiged-area2").css("z-index" ,"100").fadeIn(200).draggable();
        setPostion("#major-dialog","fixed").hide().fadeIn(200);
        $("input[type=checkbox][name=major_dataset]").change(function(){
            if($(this).get(0).checked){
                $(this).parents(".li-major:eq(0)").find(".group_checkbox:eq(0)").find("input[type=checkbox]:eq(0)").get(0).checked = true;
            }else{
                $(this).parents(".li-major:eq(0)").find(".group_checkbox:eq(0)").find("input[type=checkbox]").prop("checked",false);
            }
        });
        $("#bSelectMajor").click(function(){
            if(null != object_vars.onsubmit){
                object_vars.onsubmit();
                $('#major-dialog').fadeOut(200);
                $("input[type=checkbox][name=major_dataset]").each(function(){
                    var ch = $(this);
                    var ch_node = {"id" : ch.attr("value"),"title" : ch.attr("title"),"classnumber" : []};
                    if($(this).get(0).checked){
                        var class_year = $(this).parents(".li-major:eq(0)").find(".group_checkbox:eq(0)").find("input[type=checkbox]");
                        
                        for(var i=0;i<class_year.length;i++){
                            if( class_year[i].checked ){
                                ch_node.classnumber.push(class_year[i].value);
                            }
                        }
                        console.log(ch_node);
                        object_vars.onSelect(ch_node);
                    }
                });
                
            }
        });
    });
}

function getTeachersDialog(object_vars){
    $.post(base_url + "teacher/get_teacher_dialog",{"rnd" : Math.random()},function(data){
        $("#wiged-area2").html("");
        $(data).appendTo("#wiged-area2").fadeIn(200).draggable();
        setPostion("#teacher-dialog","fixed").css("z-index","100").hide().fadeIn(200);
        $("#bSelectTeacher").click(function(){
            if(null != object_vars.onsubmit){
                object_vars.onsubmit();
                $('#teacher-dialog').fadeOut(200);
                $("input[type=checkbox][name=teacher_dataset]").each(function(){
                    var ch = $(this);
                    if($(this).get(0).checked){
                        object_vars.onSelect({"id" : ch.attr("value"),"title" : ch.attr("title")});
                    }
                });
                
            }
        });
    });
}

function display_activity(select){
    /*
    if($(select).get(0).selectedIndex == 0){
        $(".tr_activity").fadeOut(200);
    }else{
        $(".tr_activity").fadeIn(200);
    } */
}

function callback_add_activity(){
    alert("สร้างกิจกรรมเสร็จเรียบร้อยแล้ว");
    location.reload();
    $("form[name=frm]")[0].reset();
    $("#dv-major-dataset,#dv-teacher-dataset").html("");
     editor = $("textarea").data("kendoEditor");
     editor.value("");
     
}

function _search_activity(){
      var form = $("form[name=frm]").serializeArray();
      $.post(base_url + "activity/search_activity",form,function(data){
          $("#area-display").html(data);
      });
}

function _edit_activity( activity_id ){
    $.post(base_url + "activity/get_dialog_edit_activity",{"activity_id" : activity_id ,"rand" : Math.random()},function(data){
        $("#wiged-area").html("");
        $(data).appendTo("#wiged-area").draggable({"handle" : ".skin-title"}); 
        setPostion("#edit-activity-dialog","absolute").hide().fadeIn(200);
        $("input[name=txt_code],input[name=txt_activity_score]").keypress(function(event){
           
            if(event.which && (event.which < 48 || event.which > 57 ) ){
                event.preventDefault();
            }
        });
        
    var  txt_activity_end_reg_date = $(" input[name=txt_activity_end_reg_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
           
    }});

        // วันเริ่มกิจกรรม 
        var  txt_activity_start_reg_date = $(" input[name=txt_activity_start_reg_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
                txt_activity_end_reg_date.datepicker("option", "minDate", new Date(datetext));
        }});
        //วันสิ้นสุดลงทะเบียน:
        var  txt_activity_end_date = $(" input[name=txt_activity_end_date] ").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
                txt_activity_start_reg_date.datepicker("option", "minDate", new Date(datetext));
        }});
        //วันเปิดลงทะเบียน 
        var  txt_activity_start_date = $("input[name=txt_activity_start_date]").datepicker({dateFormat:'yy-mm-dd',onSelect : function(datetext , obj){
                txt_activity_end_date.datepicker("option", "minDate", new Date(datetext));
        }});

        if($("#date_now").length > 0){
             txt_activity_start_date.datepicker("option", "minDate", new Date($("#date_now").val()));
        }
        
        $("input[type=file][name=pho]:eq(0)").change(function(){
           
            $("form[name=frm_edit_dialog]").submit();
        });
        $(".img-item-add").bind("click",function(){
            $("input[type=file][name=pho]:eq(0)").click();
            
        });
        
        $("form[name=frm_edit_dialog]").submit(function(){
            
        });
        
        $(".img-item").draggable({revert:true});
        $( "#droppable-remove" ).droppable({
			drop: function( event, ui ) {
		                       if($(ui.draggable).hasClass("img-item")){
                                                               if(!confirm("ยืนยันการลบรูปภาพกิจกรรม")) return false;
                        
                                                                $( ui.draggable ).fadeOut(800);
                                                                var  httpURLreq = base_url + "image/remove";
                                                                var id = $(ui.draggable).find("div:eq(0)").attr("title");
                                                                
                                                                $.post(httpURLreq ,{"id" : id , "rnd" : Math.random()},function( data ){
                                                                    
                                                                });
                                                                
                                                       }
			},over:function(event,ui){
                                                       $(this).css({"borderColor" : "red"});
                                                },out:function(event,ui){
                                                       $(this).css({"borderColor" : "#555"});
                                                }
         });
        
        $("#bAddMajors").click(bAddMajors_Click);
        $("#bAddTecher").click(bAddTecher_Click);
        $("input[name=txt_activity_start_date] , input[name=txt_activity_end_date] , input[name=txt_activity_start_reg_date] , input[name=txt_activity_end_reg_date]").datepicker({
            dateFormat:'yy-mm-dd'
        });
    });
}

function _submit_update_activity(){
    var form = $("form[name=frm_edit_dialog]").serializeArray();
    $.post(base_url + "activity/set_update_activity" , form , function(data){
        // alert("บันทึกข้อมูลกิจกรรมเสร็จเรียบร้อยแล้ว");
         $("#edit-activity-dialog").fadeOut(200);
         _search_activity();
    });
}
